//
//  SaleFinishViewController.m
//  PigMarket
//
//  Created by 张敬 on 2019/8/20.
//  Copyright © 2019年 pigmarket.oc. All rights reserved.
//

#import "SaleFinishViewController.h"
#import "SaleFinishCell.h"
#import "ShopSaleDetailModel.h"


@interface SaleFinishViewController ()

@property (nonatomic, strong) ShopSaleDetailModel *model;


@end

@implementation SaleFinishViewController

#pragma mark - view life cycle

- (void)setupUI {
    
    {
        self.baseNavView.hidden = YES;
    }
    
}

- (void) setupLayout {
    
    {
        [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.top.mas_equalTo(10);
            make.right.left.bottom.mas_equalTo(0);
        }];

    }
    
    {
        self.reloadBtn.mj_y -= 100;
        self.reloadLab.mj_y -= 100;
    }
}


- (void)initData{
    
    @weakify(self);
    [[[MeatShopApi sharedInstance] getMyShopSaleDetailByShopId:@1 pigId:@0 sellStatus:@3] subscribeNext:^(ShopSaleDetailModel * _Nullable model) {
        @strongify(self);
        
        self.model = model;
        if (self.isRefresh) {
            [self endRefresh];
        }
        
        if (self.model.shopList.count >0) {
            self.tableView.hidden = NO;
            [self.tableView reloadData];
        }else{
            [self loadNoData];
        }
        
    } error:^(NSError * _Nullable error) {
        @strongify(self);
        [self loadErrorData];
    }];
}

//下拉刷新
- (void)refresh
{
    [super refresh];
    [self resetData];
    [self initData];
}


#pragma mark - delegate

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.model.shopList.count;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 151;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 0;
}


- (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    return [UIView new];
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 5;
}


- (nullable UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    
    return  [UIView new];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    SaleFinishCell *cell = [tableView  dequeueReusableCellWithIdentifier:@"SaleFinishCell"];
    cell.type = indexPath.section %2 == 0 ?SaleFinishCellType_Time :SaleFinishCellType_Location;
    
    if (indexPath.section < self.model.shopList.count ) {
        ShopSaleDetailItem *item = self.model.shopList[indexPath.section];
        cell.item = item;
    }
    return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
}


#pragma mark - getter and setter

- (void)registerTableViewCell
{
    self.tableView.separatorColor = mIconLineColor;
    self.tableView.mj_footer.hidden = YES;
    [self.tableView registerClass:[SaleFinishCell class] forCellReuseIdentifier:@"SaleFinishCell"];
    
}

- (ShopSaleDetailModel *)model{
    
    if (!_model) {
        _model = [[ShopSaleDetailModel alloc] init];
    }
    return _model;
}



@end

